package com.world.rickcloudendpoint.redis.producer;

import com.world.rickcloudendpoint.stream.message.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description: redis生产者
 * @author: jlsong
 * @date: 2022/03/17 09:52:17
 * @version: 1.0
 */
@Component
public class RedisQueueProducer {


    final RedisTemplate<String, Object> redisTemplate;

    public RedisQueueProducer(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void sendMessage(String queueName, Message message, Long expire) {
        if (expire == null) {
            redisTemplate.opsForList().leftPush(queueName, message);
        } else {
            redisTemplate.opsForList().leftPush(queueName, message, expire);
        }
    }

    public void sendMessages(String queueName, List<Message> messages, Long expire) {
        if (expire == null) {
            redisTemplate.opsForList().leftPush(queueName, messages);
        } else {
            redisTemplate.opsForList().leftPush(queueName, messages, expire);
        }
    }

}
